/**
 * Project Name:dsrs-systemservice-web
 * File Name:PwdRuleController.java
 * Package Name:com.bwda.dsrs.systemservice.controller;
 * Date:2019年4月28日
 * Copyright (c) 2019, 江苏保旺达软件有限公司 All Rights Reserved.
 */
package com.bwda.dsrs.systemservice.controller;

import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.AesEncryptPaddingUtil;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.systemservice.annotations.NeedRole;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.condition.pwdrule.PwdRuleCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.form.pwdrule.PwdRuleQueryForm;
import com.bwda.dsrs.systemservice.domain.form.pwdrule.PwdRuleUpdateForm;
import com.bwda.dsrs.systemservice.domain.form.pwdrule.PwdRuleUpdateFormForEncode;
import com.bwda.dsrs.systemservice.domain.po.pwdrule.PwdRulePo;

import com.bwda.dsrs.systemservice.service.PwdRuleService;
import com.bwda.dsrs.systemservice.domain.vo.pwdrule.PwdRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 密码规则
 *
 * @author liangjianyong
 * @date 2019/4/11
 */
@RestController
@RequestMapping("/s/pwdrule")
@Api(value = "/s/pwdrule", tags = "密码规则")
public class PwdRuleController extends BaseController {

    @Autowired
    private PwdRuleService pwdRuleService;

    @Value("${aes_key}")
    private String aeskey;

    @ApiOperation(value = "查询密码规则", notes = "查询密码规则", httpMethod = "GET")
    @Loggable(descp = "查询密码规则",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<PwdRuleVo> query() throws SystemServiceException {
        PwdRuleCondition condition = new PwdRuleCondition();
        condition.setDelFlag(Constants.NOT_DELETED);
        List<PwdRulePo> poList = pwdRuleService.queryList(condition);
        List<PwdRuleVo> voList = CopyUtil.transfer(poList, PwdRuleVo.class);
        if(voList==null||voList.isEmpty()){
            return getFailResult("没有设置密码规则");
        }
        return getSuccessResult(voList.get(0));
    }

    @NeedRole("sysadminrole")
    @ApiOperation(value = "修改密码规则", notes = "修改密码规则", httpMethod = "POST")
    @Loggable(descp = "修改密码规则",type = LogTypeEnum.UPDATE,include = "")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid PwdRuleUpdateFormForEncode form) throws Exception {
        //对参数进行解密
        PwdRuleUpdateForm form1 = new PwdRuleUpdateForm();
        //解密
        if (!form.getHasNumber().isEmpty()){
            form1.setHasNumber(Integer.parseInt(AesEncryptPaddingUtil.decrypt(form.getHasNumber().toString(),aeskey)));
        }
        if (!form.getHasUpper().isEmpty()){
            form1.setHasUpper(Integer.parseInt(AesEncryptPaddingUtil.decrypt(form.getHasUpper().toString(),aeskey)));
        }
        if (!form.getHasLower().isEmpty()){
            form1.setHasLower(Integer.parseInt(AesEncryptPaddingUtil.decrypt(form.getHasLower().toString(),aeskey)));
        }
        if (!form.getHasSpecial().isEmpty()){
            form1.setHasSpecial(Integer.parseInt(AesEncryptPaddingUtil.decrypt(form.getHasSpecial().toString(),aeskey)));
        }
        if (!form.getContinuousLength().isEmpty()){
            form1.setContinuousLength(Integer.parseInt(AesEncryptPaddingUtil.decrypt(form.getContinuousLength().toString(),aeskey)));
        }
        if (!form.getHasUserName().isEmpty()){
            form1.setHasUserName(Integer.parseInt(AesEncryptPaddingUtil.decrypt(form.getHasUserName().toString(),aeskey)));
        }
        if (!form.getIsWeakPwdCheck().isEmpty()){
            form1.setIsWeakPwdCheck(Integer.parseInt(AesEncryptPaddingUtil.decrypt(form.getIsWeakPwdCheck().toString(),aeskey)));
        }
        if (!form.getHistoryPwdTime().isEmpty()){
            form1.setHistoryPwdTime(Integer.parseInt(AesEncryptPaddingUtil.decrypt(form.getHistoryPwdTime().toString(),aeskey)));
        }
        if (!form.getMinLength().isEmpty()){
            form1.setMinLength(Long.parseLong(AesEncryptPaddingUtil.decrypt(form.getMinLength().toString(),aeskey)));
        }
        if (!form.getMaxLength().isEmpty()){
            form1.setMaxLength(Long.parseLong(AesEncryptPaddingUtil.decrypt(form.getMaxLength().toString(),aeskey)));
        }
        if (!form.getValidDays().isEmpty()){
            form1.setValidDays(Integer.parseInt(AesEncryptPaddingUtil.decrypt(form.getValidDays().toString(),aeskey)));
        }
        if (!form.getNotifyDays().isEmpty()){
            form1.setNotifyDays(Integer.parseInt(AesEncryptPaddingUtil.decrypt(form.getNotifyDays().toString(),aeskey)));
        }


        PwdRulePo po = CopyUtil.transfer(form1, PwdRulePo.class);
        //查询当前密码规则
        PwdRuleCondition condition=new PwdRuleCondition();
        condition.setDelFlag(Constants.NOT_DELETED);
        List<PwdRulePo> pwdRulePoList=pwdRuleService.queryList(condition);
        if(pwdRulePoList==null||pwdRulePoList.isEmpty()){
            //新增
            pwdRuleService.insert(po);
        }else{
            //修改
            Integer id=pwdRulePoList.get(0).getId();
            po.setId(id);
            pwdRuleService.update(po);
        }
        return getSuccessResult();
    }




    /**
     * PwdRuleQueryForm转换为PwdRuleCondition
     *
     * @param form
     * @return
     */
    private PwdRuleCondition getConditionByQueryForm(PwdRuleQueryForm form) {
        PwdRuleCondition condition = CopyUtil.transfer(form, PwdRuleCondition.class);
        return condition;
    }

}